<template>
  <!-- 新增角色对话框 -->
  <el-dialog title="新增地域" width="600px" :visible.sync="dialogVisible">
    <el-form
      ref="addForm"
      :model="formData"
      :rules="rules"
      size="medium"
      label-width="80px"
    >
      <el-row>
        <el-col :span="24">
          <el-form-item label="上级地域" prop="parentId">
            <treeselect
              v-model="formData.parentId"
              :options="areaOptions"
              :normalizer="normalizer"
              placeholder="请选择上级地域"
            />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="地域名称" prop="areaName">
            <el-input
              v-model="formData.areaName"
              placeholder="请输入地域名称"
              clearable
            ></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="负责人" prop="leader">
            <el-input
              v-model="formData.leader"
              placeholder="请输入负责人"
              clearable
            ></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="联系方式" prop="phone">
            <el-input
              v-model="formData.phone"
              placeholder="请输入联系方式"
              clearable
            >
            </el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="邮箱" prop="email">
            <el-input
              v-model="formData.email"
              placeholder="请输入邮箱"
              clearable
            >
            </el-input>
          </el-form-item>
        </el-col>
        <!-- <el-col :span="12">
          <el-form-item label="状态" prop="status">
            <el-radio-group v-model="formData.status" size="medium">
              <el-radio
                v-for="(item, index) in statusOptions"
                :key="index"
                :label="item.value"
              >
                {{ item.label }}
              </el-radio>
            </el-radio-group>
          </el-form-item>
        </el-col> -->
        <el-col :span="24">
          <el-form-item label="备注">
            <el-input
              v-model="formData.areaDesc"
              type="textarea"
              placeholder="请输入内容"
              :autosize="{ minRows: 3, maxRows: 4 }"
            ></el-input>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="dialogVisible = false">取 消</el-button>
      <el-button type="primary" @click="submitAdd">
        确 定
      </el-button>
    </div>
  </el-dialog>
</template>

<script>
import { addArea } from '@/api/system/area';
import Treeselect from '@riophae/vue-treeselect';
import '@riophae/vue-treeselect/dist/vue-treeselect.css';
export default {
  name: 'AddDialog',
  components: { Treeselect },
  data() {
    return {
      formData: {
        parentId: null,
        areaName: '',
        leader: '',
        phone: '',
        email: '',
        areaDesc: ''
      },
      rules: {
        areaName: [
          { required: true, message: '请输入地域名称', trigger: 'blur' }
        ],
        email: [
          {
            type: 'email',
            message: '请输入正确的邮箱地址',
            trigger: ['blur', 'change']
          }
        ]
      }
    };
  },
  computed: {
    dialogVisible: {
      get() {
        return this.$parent.dialogAddVisible;
      },
      set(v) {
        this.$parent.dialogAddVisible = v;
      }
    },
    areaOptions() {
      return this.$parent.tableData;
    }
  },
  methods: {
    normalizer(node) {
      if (node.children && !node.children.length) {
        delete node.children;
      }
      return {
        id: node.areaId,
        label: node.areaName,
        children: node.children
      };
    },
    /**
     * 提交新增对话框
     */
    submitAdd() {
      this.$refs['addForm'].validate(async valid => {
        if (!valid) return;
        console.log('添加地域表单', this.formData);
        const { data: res } = await addArea(this.formData);
        console.log('api-添加地域', res);
        if (res.code === 200) {
          this.$message.success('添加地域成功');
          this.dialogVisible = false;
          this.$parent.getAllarea();
        } else {
          this.$message.error(res.message);
        }
      });
    }
  }
};
</script>

<style lang="scss" scoped></style>
